"""
定义一个函数，输入一个链表的头节点，反转该链表并输出反转后链表的头节点。
示例:
输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL
链接：https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof
"""
from mode import *


# 返回链表节点
class Solution:
    # 返回ListNode
    def reversePrint(self, pHead):
        # write code here
        if not pHead or not pHead.next:
            return pHead

        p, q = pHead, None
        while p:
            temp = p.next
            p.next = q
            q = p
            p = temp
        return q


class Solution1:
    # 返回ListNode
    def reversePrint(self, head):
        if not head:
            return None
        pre, cur = None, head

        while cur:
            temp = cur.next
            cur.next = pre
            pre = cur
            cur = temp

        return pre


if __name__ == "__main__":
    """
    输入：head = [1,3,2]
    输出：[2,3,1]
    """
    a1 = ListNode(1)
    a2 = ListNode(2)
    a3 = ListNode(3)
    a1.next = a2
    a2.next = a3

    A = Solution1()
    A.reversePrint(a1)
    PrintListNode(a3)
